package com.firmboy.big;

import com.firmboy.staticdb.DBPoolHelper;
import com.firmboy.threadPool.PoolFactory;
import org.junit.Test;
import org.junit.internal.runners.statements.RunAfters;

import java.io.*;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author firmboy
 * @create 2018-04-23 上午11:12
 **/
public class ExpData {


    @Test
    public void ExpData(){
        final Long count = 4000000L;
        final int pageSize = 10000;
        List<Map<String, Object>> list = null;

        Long page = 0L;
        if(count%Long.valueOf(pageSize)==0){
            page = count/Long.valueOf(pageSize);
        }else{
            page = count/Long.valueOf(pageSize)+1L;
        }


        String path = this.getClass().getResource("/").getPath();
        String replace = this.getClass().getName().replace(".", "/");
        replace = replace.substring(0,replace.lastIndexOf("/")+1);
        path = path.substring(0,path.indexOf("/target")+1)+"src/main/java/";
        path = path.substring(0,path.lastIndexOf("/")+1)+replace;
        String fileName = "test.db";

        File file = new File(path+fileName);
        if(file.exists()){
            file.delete();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileWriter fw = null;
        try {
            StringBuffer sb = new StringBuffer();
            fw = new FileWriter(file);
            for (Long currPage=0L;currPage<page;currPage++){
                System.out.println("当前处理第"+currPage+"页");
                list = DBPoolHelper.getListObject("select t2.* From (SELECT id FROM test_user LIMIT ?,?) t1 left join test_user t2 on t1.id= t2.id", new Object[]{currPage*pageSize, pageSize});
                for(int i = 0;i<list.size();i++){
                    Map<String,Object> map = list.get(i);
                    sb.append("userName:"+map.get("user_name").toString()+",");
                    sb.append("passWord:"+map.get("pass_word").toString()+",");
                    fw.append(sb.toString()+"\r\n");
                    sb = new StringBuffer();
                }
            }



        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fw!=null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Test
    public void impData(){

        String path = this.getClass().getResource("/").getPath();
        String replace = this.getClass().getName().replace(".", "/");
        replace = replace.substring(0,replace.lastIndexOf("/")+1);
        path = path.substring(0,path.indexOf("/target")+1)+"src/main/java/";
        path = path.substring(0,path.lastIndexOf("/")+1)+replace;
        String fileName = "test.db";

        File file = new File(path+fileName);
        FileReader fi = null;
        BufferedReader bfi = null;
        String str = null;
        Long i = 0L;
        try {
             fi = new FileReader(file);
             bfi = new BufferedReader(fi);
             while((str = bfi.readLine())!=null){
                 i++;
                 System.out.println(i+":"+str);
             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bfi!=null){
                try {
                    bfi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fi!=null){
                try {
                    fi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @Test
    public void test(){
        //List<Map<String, Object>> list = DBPoolHelper.getListObject("SELECT * FROM test_user t1 left JOIN (SELECT id FROM test_user LIMIT ?,?)t2 on t1.id = t2.id", new Object[]{0, 100});
        //List<Map<String, Object>> list = DBPoolHelper.getListObject("SELECT * FROM test_user limit ?,?", new Object[]{0, 100});
        long start = System.currentTimeMillis();
        List<Map<String, Object>> list = DBPoolHelper.getListObject("select t2.* From (SELECT id FROM test_user LIMIT ?,?) t1 left join test_user t2 on t1.id= t2.id", new Object[]{0, 1000});
        System.out.println(list.size());
        long end = System.currentTimeMillis();
        System.out.println("耗时："+(end-start));

    }


    @Test
    public void ExpDataThread(){
        //开启20个线程，利用数据库连接池，多线程的操作数据，具体的思路是，先每个线程写文件到各自线程的文件中去，并且，要根据当前的页码来命名
        //这样是为了待会和成一个文件时，保持数据的顺序，
        //具体的思路每页10000行数据，这样就差不多有500个文件，
        //操作完成之后，要将这些文件删除，
        //现在的问题是怎样保证线程池中的数据执行完成之后，主线程开始

        final AtomicInteger curPage = new AtomicInteger(1);
        final Long count = 10000L;
        final int pageSize = 100;
        List<Map<String, Object>> list = null;
        final String path = getPath()+"/temp"+System.currentTimeMillis()+"/";

        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }

        Long pageTemp = 0L;
        if(count%Long.valueOf(pageSize)==0){
            pageTemp = count/Long.valueOf(pageSize);
        }else{
            pageTemp = count/Long.valueOf(pageSize)+1L;
        }
        final Long page = pageTemp;

        //创建线程池
        ThreadPoolExecutor pool = PoolFactory.getPool();

        CountDownLatch countDownLatch = new CountDownLatch(page.intValue());

        Runnable my = new Runnable() {
            @Override
            public void run() {
                int i = curPage.getAndAdd(1);
                if(i<=page){
                    List<Map<String, Object>> list = DBPoolHelper.getListObject("select t2.* From (SELECT id FROM test_user LIMIT ?,?) t1 left join test_user t2 on t1.id= t2.id", new Object[]{(i-1)*pageSize, pageSize});
                    System.out.println("第"+i+"查询完成");

                    String fileName = i+".db";
                    File file = new File(path + fileName);
                    if(!file.exists()){
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    FileWriter fw = null;
                    try {
                         fw = new FileWriter(file);
                        StringBuffer sb = new StringBuffer();
                        for (int j = 0;j<list.size();j++){
                            Map<String,Object> map = list.get(j);
                            sb.append("userName:"+map.get("user_name").toString());
                            sb.append("passWord:"+map.get("pass_word").toString());
                            fw.write(sb.toString()+"\r\n");
                            sb = new StringBuffer();
                         }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        if(fw!=null){
                            try {
                                fw.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        countDownLatch.countDown();
                    }

                }


            }
        };

        for (int i =0;i<page;i++){
            pool.execute(my);
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("程序结束 ，bye");

    }


    private String getPath(){
        String path = this.getClass().getResource("/").getPath();
        String replace = this.getClass().getName().replace(".", "/");
        replace = replace.substring(0,replace.lastIndexOf("/")+1);
        path = path.substring(0,path.indexOf("/target")+1)+"src/main/java/";
        path = path.substring(0,path.lastIndexOf("/")+1)+replace;

        return path;
    }

}
